package com.homedo.odin.solution.service.bizservice.mng;

import com.homedo.core.common.base.dto.Meta;
import com.homedo.core.common.exception.BizException;
import com.homedo.core.common.util.BeanUtils;
import com.homedo.core.common.util.NumberHelper;
import com.homedo.odin.solution.contract.mng.CommonDataEditReq;
import com.homedo.odin.solution.contract.mng.CommonDataListEditReq;
import com.homedo.odin.solution.contract.mng.CommonTypeDataEditReq;
import com.homedo.odin.solution.contract.mng.CommonTypeDataPageResp;
import com.homedo.odin.solution.contract.mng.CommonTypeDataReq;
import com.homedo.odin.solution.contract.mng.CommonTypeDataResp;
import com.homedo.odin.solution.enumcode.HighlightEnum;
import com.homedo.odin.solution.enumcode.SqlEnum;
import com.homedo.odin.solution.enumcode.basic.CommonTypeDataEnum;
import com.homedo.odin.solution.enumcode.basic.ErrorCodeEnum;
import com.homedo.odin.solution.model.dto.CommonTypeDataResponseBean;
import com.homedo.odin.solution.model.entity.CommonTypeData;
import com.homedo.odin.solution.service.CommonTypeDataServiceImpl;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: zhaoxudong
 * @date: 2018/4/11 14:44
 * @Description:
 */
@Service
public class MngCommonBizService {

    @Autowired
    CommonTypeDataServiceImpl commonTypeDataService;

    public CommonTypeDataResp nodes(Integer id) {
        List<CommonTypeData> list = commonTypeDataService.queryChildNodeById(id);
        CommonTypeDataResp resp = new CommonTypeDataResp();
        resp.setList(BeanUtils.batchTransform(CommonTypeDataResponseBean.class, list));
        return resp;
    }

    public void edit(CommonDataEditReq req) {
        Integer count = commonTypeDataService.editCommonDataById(req.getId(), req.getSort(), req.getName());
        if (count <= 0) {
            throw new BizException(SqlEnum.FAIL.getCode(), "修改失败");
        }
    }

    public void add(CommonTypeDataReq commonTypeDataRequest) {
        if (!NumberHelper.validate(commonTypeDataRequest.getParentId()) ||
                StringUtils.isEmpty(commonTypeDataRequest.getName())) {
            throw new BizException(Meta.CODE_451.getCode(), "参数不正确！");
        }
        CommonTypeData addcommonTypeData = new CommonTypeData();
        if (commonTypeDataRequest.getParentId() != 0) {
            CommonTypeData commonTypeData = commonTypeDataService
                    .getCommonTypeDataById(commonTypeDataRequest.getParentId());
            if (commonTypeData == null) {
                throw new BizException(ErrorCodeEnum.NotExist.getDbValue(), "关联的父亲节点不存在!");
            }
            List<CommonTypeData> dataList = commonTypeDataService
                    .queryDataByName(commonTypeDataRequest.getName(), commonTypeDataRequest.getParentId());
            if (CollectionUtils.isNotEmpty(dataList)) {
                throw new BizException(ErrorCodeEnum.IsExist.getDbValue(), "该分类名称已存在!");
            }
            addcommonTypeData.setName(commonTypeDataRequest.getName());
            addcommonTypeData.setParentId(commonTypeDataRequest.getParentId());
            Integer id = commonTypeDataService.addCommonTypeData(addcommonTypeData);
            addcommonTypeData.setId(id);
            addcommonTypeData.setDepth(commonTypeData.getDepth() + 1);
            addcommonTypeData.setPath(String.format("%s,%s", commonTypeData.getPath(), id));
        } else {
            addcommonTypeData.setName(commonTypeDataRequest.getName());
            addcommonTypeData.setParentId(commonTypeDataRequest.getParentId());
            Integer id = commonTypeDataService.addCommonTypeData(addcommonTypeData);
            addcommonTypeData.setId(id);
            addcommonTypeData.setDepth(1);
            addcommonTypeData.setPath(id.toString());
        }
        commonTypeDataService.updateCommonTypeData(addcommonTypeData);
    }

    public void del(Integer id) {
        CommonTypeData commonTypeData = commonTypeDataService.getCommonTypeDataById(id);
        if (commonTypeData == null) {
            throw new BizException(ErrorCodeEnum.NotExist.getDbValue(), "该节点不存在!");
        }
        //删除该节点以及它下面所有的子节点
        List<CommonTypeData> childList = commonTypeDataService.queryChildNodeById(id);
        if (CollectionUtils.isNotEmpty(childList)) {
            throw new BizException(ErrorCodeEnum.NotSupport.getDbValue(), "该节点为父节点无法删除!");
        }
        commonTypeDataService.deleteCommonDataById(commonTypeData);
    }

    /**
     * @Description: OMS_批量修改传递的基础数据为选中
     * @Author: liyi
     */
    public void batchHightLight(Integer id, CommonDataListEditReq req) {
        List<CommonDataEditReq> list = req.getList();
        if (list.size() > 0) {
            //将业态和系统之前所有的设置成不高亮
            commonTypeDataService.updateCommonDataNoHighlightByParentId(id, HighlightEnum.NO.getDbValue());
            //将用户传递的ids指定值设置成高亮
            for (CommonDataEditReq commonDataEditReq : list) {
                commonTypeDataService
                        .updateCommonDataNoHighlightById(commonDataEditReq.getId(), HighlightEnum.YES.getDbValue());
            }
        }
    }

    public void hightlight(CommonTypeDataEditReq req) {
        CommonTypeData commonTypeData = BeanUtils.transfrom(CommonTypeData.class, req);
        commonTypeData.setLastModifiedTime(new Date());
        commonTypeDataService.insertOrUpdate(commonTypeData);
    }


    /**
     * @Description: 获取系统和业态高亮词
     * @Author: liyi
     */
    public CommonTypeDataPageResp highCommonData() {
        CommonTypeDataPageResp resp = new CommonTypeDataPageResp();
        List<CommonTypeDataResponseBean> resultCommonTypeDataResponseBean = new ArrayList<CommonTypeDataResponseBean>();
        //业态属性
        List<CommonTypeData> jzytList = commonTypeDataService
                .queryChildNodeBy(CommonTypeDataEnum.JZYT.getDbValue(), HighlightEnum.YES.getDbValue());
        List<CommonTypeDataResponseBean> jzytCommonTypeDataList = (BeanUtils
                .batchTransform(CommonTypeDataResponseBean.class, jzytList));

        //一级系统属性
        List<CommonTypeData> yjxtsxList = commonTypeDataService
                .queryChildNodeBy(CommonTypeDataEnum.YJXTSX.getDbValue(), HighlightEnum.YES.getDbValue());
        List<CommonTypeDataResponseBean> yjxtsxCommonTypeDataList = (BeanUtils
                .batchTransform(CommonTypeDataResponseBean.class, yjxtsxList));

        if (jzytList.size() > 0) {
            for (CommonTypeDataResponseBean commonTypeData : jzytCommonTypeDataList) {
                resultCommonTypeDataResponseBean.add(commonTypeData);
            }
        }

        if (yjxtsxList.size() > 0) {
            for (CommonTypeDataResponseBean commonTypeData : yjxtsxCommonTypeDataList) {
                resultCommonTypeDataResponseBean.add(commonTypeData);
            }
        }

        resp.setList(resultCommonTypeDataResponseBean);
        //获取业态或系统的最近修改时间和修改人
        CommonTypeData commonTypeData = commonTypeDataService.queryLateCommonTypeData();
        resp.setModifiedTime(commonTypeData.getLastModifiedTime());
        resp.setModifiedBy(commonTypeData.getModifiedBy());
        resp.setOperator(commonTypeData.getOperator());
        return resp;
    }

    /**
     * 根据id获取可用的基础数据管理
     */
    public CommonTypeDataResp important(Integer id) {
        List<CommonTypeData> list = commonTypeDataService.queryChildNodeById(id);
        CommonTypeDataResp resp = new CommonTypeDataResp();
        resp.setList(BeanUtils.batchTransform(CommonTypeDataResponseBean.class, list));
        return resp;
    }
}
